package processinghelper;

import java.util.List;


import processing.core.PApplet;
import processing.core.PGraphics;
import processing.core.PVector;

public class PGraphicsHelper {
	public static String timestamp(PApplet p){
		return p.year() + p.nf( p.month(),2) +  p.nf( p.day(),2) + "-"  +  p.nf( p.hour(),2) +  p.nf( p.minute(),2) +   p.nf( p.second(),2);
	}
	public static void drawExtrude(List<PVector> points, PVector extrude,
			PGraphics pg) {
	}

	public static void drawFace(List<PVector> points, PGraphics pg) {
		pg.beginShape();
		for (int i = 0; i < points.size(); i++) {
			PVector p = points.get(i);
			pg.vertex(p.x, p.y, p.z);
		}
		pg.endShape(PGraphics.CLOSE);
	}

	public static void drawExtrudePolyLine(List<PVector> points,
			PVector e, PGraphics pg) {
		pg.beginShape(PGraphics.QUAD_STRIP);
		for (int i = 0; i <= points.size(); i++) {
			PVector p = points.get(i % (points.size()));
			pg.vertex(p.x, p.y, p.z);
			pg.vertex(p.x+e.x, p.y+e.y, p.z+e.z);
		}
		pg.endShape();
	}
	
	public static void drawExtrudeLine(float x1, float y1, float x2, float y2,
			float z1, float z2, PGraphics pg) {
		pg.beginShape(PGraphics.QUAD);
		pg.vertex(x1, y1, z1);
		pg.vertex(x2, y2, z1);
		pg.vertex(x2, y2, z2);
		pg.vertex(x1, y1, z2);
		pg.endShape();
	}
	public static void drawExtrudeVerticalLine(float x1, float y1,float z1, float x2, float y2,float z2,
			float h1,float h2, PGraphics pg) {
		pg.beginShape(PGraphics.QUAD);
		pg.vertex(x1, y1, z1);
		pg.vertex(x2, y2, z2);
		pg.vertex(x2, y2, z2+h2);
		pg.vertex(x1, y1, z1+h1);
		pg.endShape();
	}

	public static void drawExtrudeLine(PVector p1, PVector p2, PVector extrude,
			PGraphics pg) {

	}

	public static void drawBox(float x, float y, float z, float dX, float dY,
			float dZ, PGraphics pg) {
		pg.pushMatrix();
		pg.translate(x, y, z);
		pg.box(dX, dY, dZ);
		pg.popMatrix();
	}

	public static void drawBoxByCorner(float x, float y, float z, float dX,
			float dY, float dZ, PGraphics pg) {
		pg.pushMatrix();
		pg.translate(x + dX * 0.5f, y + dY * 0.5f, z + dZ * 0.5f);
		pg.box(dX, dY, dZ);
		pg.popMatrix();
	}

	public static void drawExtrudeVerticalLine(float x1, float y1, float x2,
			float y2, float z1, float z2, PGraphics pg) {

		pg.beginShape(PGraphics.QUAD);
		pg.vertex(x1, y1, z1);
		pg.vertex(x2, y2, z1);
		pg.vertex(x2, y2, z2);
		pg.vertex(x1, y1, z2);
		pg.endShape();
	}
	public static void drawExtrudeVerticalLine(double x1, double y1, double x2,
			double y2, double z1, double z2, PGraphics pg) {

		drawExtrudeVerticalLine((float)x1,(float)y1,(float)x2,(float)y2,(float)z1,(float)z2,pg);
	}

	public static void drawExtrudeVerticalLine(float x1, float y1, float x2,
			float y2, float h, PGraphics pg) {
		drawExtrudeVerticalLine(x1, y1, x2, y2, 0, h, pg);
	}

	public static void drawExtrudeVerticalLine(float l, float h, PGraphics pg) {
		drawExtrudeVerticalLine(0, 0, l, 0, 0, h, pg);
	}

	public static void drawExtrude(List<PVector> points, float z,
			float height, PGraphics pg) {
		drawHorPoly(points, z, pg);
		drawExtrudePolyLine(points, z, z + height, pg);
		drawHorPoly(points, z + height, pg);
	}
	
	
	
	public static void drawExtrudeQuad(PVector p1,PVector p2,PVector p3,PVector p4, float z1,
			float z2, PGraphics pg) {

		pg.beginShape(PGraphics.QUAD_STRIP);
		
			// System.out.println("i:"+(i%(points.size())) );
			pg.vertex(p1.x, p1.y, z1);
			pg.vertex(p1.x, p1.y, z2);
			pg.vertex(p2.x, p2.y, z1);
			pg.vertex(p2.x, p2.y, z2);
			pg.vertex(p3.x, p3.y, z1);
			pg.vertex(p3.x, p3.y, z2);
			pg.vertex(p4.x, p4.y, z1);
			pg.vertex(p4.x, p4.y, z2);
			pg.vertex(p1.x, p1.y, z1);
			pg.vertex(p1.x, p1.y, z2);
		
		pg.endShape();
		drawHorQuad(p1,p2,p3,p4,z1,pg);
		drawHorQuad(p1,p2,p3,p4,z2,pg);
	}
	
	public static void drawHorQuad(PVector p1,PVector p2,PVector p3,PVector p4, float z1,PGraphics pg) {
		pg.beginShape(PGraphics.QUADS);
		pg.vertex(p1.x, p1.y, z1);
		pg.vertex(p2.x, p2.y, z1);
		pg.vertex(p3.x, p3.y, z1);
		pg.vertex(p4.x, p4.y, z1);
		pg.endShape();
	}
	
	public static void drawHorPoly(List<? extends PVector> points, float z, PGraphics pg) {
		pg.beginShape();
		for (int i = 0; i < points.size(); i++) {
			PVector p = points.get(i);
			pg.vertex(p.x, p.y, z);
		}
		pg.endShape(PGraphics.CLOSE);
	}

	public static void drawExtrudePolyLine(List<? extends PVector> points, float z1,
			float z2, PGraphics pg) {

		pg.beginShape(PGraphics.QUAD_STRIP);
		for (int i = 0; i <= points.size(); i++) {
			PVector p = points.get(i % (points.size()));
			// System.out.println("i:"+(i%(points.size())) );
			pg.vertex(p.x, p.y, z1);
			pg.vertex(p.x, p.y, z2);
		}
		pg.endShape();
	}
	
	
	public static void drawFrame(float x, float y, float z, float dX, float dY,
			float dZ, float fTop, float fDown, float fLeft, float fRight,
			PGraphics pg) {
		drawBoxByCorner(x, y, z, dX, dY, fDown, pg); // bottom
		drawBoxByCorner(x, y, z + dZ - fTop, dX, dY, fTop, pg);// top
		drawBoxByCorner(x, y, z + fDown, fLeft, dY, dZ - (fTop + fDown), pg);// left
		drawBoxByCorner(x + dX - fRight, y, z + fDown, fRight, dY, dZ
				- (fTop + fDown), pg);// right
	}
	public static void  drawWorldAxis3D(PGraphics pg,float scale){
		pg.pushStyle();
		pg.stroke(255,0,0);
		pg.line(0,0,0,scale,0,0);
		pg.stroke(0,255,0);
		pg.line(0,0,0,0,scale,0);
		pg.stroke(0,0,255);
		pg.line(0,0,0,0,0,scale);
		pg.popStyle();
	}
	
	
	
	public static void  writeText(PGraphics g, String t, float ss) {
		Float v[][] = { { 0.0f, 0.0f, .5f, 0.0f }, { .5f, 0.0f, 1.0f, 0.0f },
				{ 1.0f, 0.0f, 1.0f, .5f }, { 1.0f, .5f, 1.0f, 1.0f },
				{ 1.0f, 1.0f, .5f, 1.0f }, { .5f, 1.0f, 0.0f, 1.0f },
				{ 0.0f, 1.0f, 0.0f, .5f }, { 0.0f, .5f, 0.0f, 0.0f },
				{ .5f, .5f, 0.0f, 0.0f }, { .5f, .5f, .5f, 0.0f },
				{ .5f, .5f, 1.0f, 0.0f }, { .5f, .5f, 1.0f, .5f },
				{ .5f, .5f, 1.0f, 1.0f }, { .5f, .5f, .5f, 1.0f },
				{ .5f, .5f, 0.0f, 1.0f }, { .5f, .5f, 0.0f, .5f } };
		float seg[] = new float[16];
	
		float le = (float) (t.length()) / 2.0f;
		for (int c = 0; c < t.length(); c++) {
			String l = t.substring(c, c + 1);
			for (int d = 0; d < 48; d++) {
				if (l.equals(b[d][0])) {
					String code = b[d][1];
					for (int e = 0; e < 16; e++) {
						String di = code.substring(e, e + 1);
						if (di.equals("1")) {
							seg[e] = 0;
						} else {
							seg[e] = 1;
						}
					}
					for (int e = 0; e < 16; e++) {
						if (seg[e] == 1) {
							g.line((c * 1.3f + v[e][0] - le) * ss, (-v[e][1])
									* ss, (c * 1.3f + v[e][2] - le) * ss,
									(-v[e][3]) * ss);
						}
					}
				}
			}
		}
	
	}
	
	static String b[][] = { { "!", "1100111111111111" },
			{ "#", "1100001110101010" }, { "$", "0010001010101010" },
			{ "%", "0110011010001000" }, { "&", "1010001110011001" },
			{ "(", "1011011110111011" }, { ")", "0111101110111011" },
			{ "*", "1111111100000000" }, { "+", "1111111110101010" },
			{ ",", "1111111111111101" }, { "-", "1111111111101110" },
			{ ".", "1111100111111010" }, { "/", "1111111111011101" },
			{ "0", "0000000011111111" }, { "1", "1100111111011111" },
			{ "2", "0001000111101110" }, { "3", "0000001111101111" },
			{ "4", "1100111011101110" }, { "5", "0010001011101110" },
			{ "6", "0010000011101110" }, { "7", "0011111111011011" },
			{ "8", "0000000011101110" }, { "9", "0000001011101110" },
			{ "A", "0000110011101110" }, { "B", "0000001110101011" },
			{ "C", "0011000011111111" }, { "D", "0000001110111011" },
			{ "E", "0011000011101110" }, { "F", "0011110011111110" },
			{ "G", "0010000011101111" }, { "H", "1100110011101110" },
			{ "I", "1111111110111011" }, { "J", "1000000111111111" },
			{ "K", "1110110011001110" }, { "L", "1111000011111111" },
			{ "M", "1100110001011111" }, { "N", "1100110001110111" },
			{ "O", "0000000011111111" }, { "P", "0001110011101110" },
			{ "Q", "0000000011110111" }, { "R", "0001110011100110" },
			{ "S", "0010001011101110" }, { "T", "0011111110111011" },
			{ "U", "1100000011111111" }, { "V", "1111110011011101" },
			{ "W", "1100110011110101" }, { "X", "1111111101010101" },
			{ "Y", "1111111101011011" }, { "Z", "0011001111011101" } };

}
